﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TempCtr.MyControl;

namespace TempCtr.Model
{
    public class Thermostat
    {
        public static short highTemp;
        public static short lowTemp;      
        #region 静态属性
        public static ThermostatSerialPort thermostat = new ThermostatSerialPort();
        public static Dictionary<int, Thermostat> ThermostatDic = new Dictionary<int, Thermostat>();
        private static System.Timers.Timer Time1 = new System.Timers.Timer();
        private static System.Timers.Timer Time2 = new System.Timers.Timer();
        private static System.Timers.Timer Time3 = new System.Timers.Timer();
        private static System.Timers.Timer Time4 = new System.Timers.Timer();
        private static System.Timers.Timer Time5 = new System.Timers.Timer();
        private static System.Timers.Timer Time6 = new System.Timers.Timer();
        private static System.Timers.Timer Time7 = new System.Timers.Timer();
        private static System.Timers.Timer Time8 = new System.Timers.Timer();
        private static System.Timers.Timer Time9 = new System.Timers.Timer();
        private static System.Timers.Timer Time10 = new System.Timers.Timer();

        private static System.Timers.Timer ImforTime = new System.Timers.Timer();

        public static int MaxTimeOutCount = 12;

        public static int ccck;

        public static bool IsOpen1 = false;
        public static bool IsOpen2 = false;
        public static bool IsOpen3 = false;
        public static bool IsOpen4 = false;
        public static bool IsOpen5 = false;
        public static bool IsOpen6 = false;
        public static bool IsOpen7 = false;
        public static bool IsOpen8 = false;
        public static bool IsOpen9 = false;
        public static bool IsOpen10 = false;
        #endregion

        #region 属性
        private int _iD =0x01;
        private byte _addr = 0x00;
        private string _num="1-3";
        private string _com = "COM1";
        private bool _isConnet = true;
        private int _timeOutCount = 0;
        private double _temp = 0.0;
        private double _maxtemp = -20.0;
        private double _mintemp = 10.0;
        private TempCard _card;

        public int ID { get => _iD; set => _iD = value; }
        public byte Addr { get => _addr; set => _addr = value; }
        public string Num { get => _num; set => _num = value; }

        public string Com { get => _com; set => _com = value; }
        public bool IsConnet
        {
            get => _isConnet; set
            {
                if (_isConnet == value) return;
                _isConnet = value;
                ThermostatDic[Addr].TimeOutCount = 0;
                
            }
        }
        public int TimeOutCount { get => _timeOutCount; set => _timeOutCount = value; }
        public double Temp { get => _temp; set => _temp = value; }
        public double MaxTemp { get => _maxtemp; set => _maxtemp = value; }
        public double MinTemp { get => _mintemp; set => _mintemp = value; }
        public TempCard Card { get => _card; set => _card = value; }
        #endregion

        public static SerialPort SP1 = new SerialPort();
        private static SerialPort SP2 = new SerialPort();
        private static SerialPort SP3 = new SerialPort();
        private static SerialPort SP4 = new SerialPort();
        private static SerialPort SP5 = new SerialPort();
        private static SerialPort SP6 = new SerialPort();
        private static SerialPort SP7 = new SerialPort();
        private static SerialPort SP8 = new SerialPort();
        private static SerialPort SP9 = new SerialPort();
        private static SerialPort SP10 = new SerialPort();

        public Thermostat(int id, string num,byte addr,string com)
        {
            ID = id;
            Num = num;
            Addr = addr;
            Card = new TempCard(this);
            Com = com;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            //初始化温控仪列表
            UPThermostat();          
            var BaudRate = Properties.Settings.Default.Port_BaudRate;
            var Scan_Interval = Properties.Settings.Default.Scan_Interval;

            if (ThermostatDic.Keys.Count != 0 ) ccck = ThermostatDic.Keys.Max() / 33 + 1;
            else ccck = 0;

            if (ccck > 0)
            {
                IsOpen1 = thermostat.Connect(SP1, ThermostatDic[1].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen1)
                {
                    SP1.DataReceived += SP1_DataReceived;
                    Time1.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time1.Elapsed += Timing1_Query;//到达时间的时候执行事件；           
                    Time1.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time1.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；

                    ImforTime.Interval = 30 * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    ImforTime.Elapsed += Imfor_Query;//到达时间的时候执行事件；           
                    ImforTime.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    ImforTime.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 1)
            {
                IsOpen2 = thermostat.Connect(SP2, ThermostatDic[33].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen2)
                {
                    SP2.DataReceived += SP2_DataReceived;
                    Time2.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time2.Elapsed += Timing2_Query;//到达时间的时候执行事件；           
                    Time2.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time2.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 2)
            {
                IsOpen3 = thermostat.Connect(SP3, ThermostatDic[65].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen3)
                {
                    SP3.DataReceived += SP3_DataReceived;
                    Time3.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time3.Elapsed += Timing3_Query;//到达时间的时候执行事件；           
                    Time3.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time3.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 3)
            {
                IsOpen4 = thermostat.Connect(SP4, ThermostatDic[97].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen4)
                {
                    SP4.DataReceived += SP4_DataReceived;
                    Time4.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time4.Elapsed += Timing4_Query;//到达时间的时候执行事件；           
                    Time4.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time4.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 4)
            {
                IsOpen5 = thermostat.Connect(SP5, ThermostatDic[129].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen5)
                {
                    SP5.DataReceived += SP5_DataReceived;
                    Time5.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time5.Elapsed += Timing5_Query;//到达时间的时候执行事件；           
                    Time5.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time5.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 5)
            {
                IsOpen6 = thermostat.Connect(SP6, ThermostatDic[5*32+1].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen6)
                {
                    SP6.DataReceived += SP6_DataReceived;
                    Time6.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time6.Elapsed += Timing6_Query;//到达时间的时候执行事件；           
                    Time6.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time6.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 6)
            {
                IsOpen7 = thermostat.Connect(SP7, ThermostatDic[6*32+1].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen7)
                {
                    SP7.DataReceived += SP7_DataReceived;
                    Time7.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time7.Elapsed += Timing7_Query;//到达时间的时候执行事件；           
                    Time7.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time7.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 7)
            {
                IsOpen8 = thermostat.Connect(SP8, ThermostatDic[7*32+1].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen8)
                {
                    SP8.DataReceived += SP8_DataReceived;
                    Time8.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time8.Elapsed += Timing8_Query;//到达时间的时候执行事件；           
                    Time8.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time8.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 8)
            {
                IsOpen9 = thermostat.Connect(SP9, ThermostatDic[8*32+1].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen9)
                {
                    SP9.DataReceived += SP9_DataReceived;
                    Time9.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time9.Elapsed += Timing9_Query;//到达时间的时候执行事件；           
                    Time9.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time9.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }
            if (ccck > 9)
            {
                IsOpen10 = thermostat.Connect(SP10, ThermostatDic[9*32+1].Com, BaudRate);  //isopen是否需要加到10个
                if (IsOpen10)
                {
                    SP10.DataReceived += SP1_DataReceived;
                    Time10.Interval = Scan_Interval * 1000;//实例化Timer类，设置间隔时间为X * 1000毫秒；
                    Time10.Elapsed += Timing10_Query;//到达时间的时候执行事件；           
                    Time10.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
                    Time10.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
                }
            }       
        }

       

        private static void WaitingTimeOutEvent(int ID, byte Cmd)
        {
            Console.WriteLine("地址：  " + ID + "超时，命令为： " + Cmd);
            //MainFrom.Logger.Info("地址：  " + ID + "超时，命令为： " + Cmd);

            if (++ThermostatDic[ID].TimeOutCount == MaxTimeOutCount)
            {
                //掉线            
                ThermostatDic[ID].IsConnet = false;
                ThermostatDic[ID].Card.UpShowOnline(false);
                ThermostatDic[ID].Card.UpShowFirst();
                //MySqlHelp.AddLog("温度模块", "短信报警", "温控仪" + ThermostatDic[Addr].ID + "连接失败");
            }
        }

        /// <summary>
        /// 更新温控仪列表
        /// </summary>
        public static void UPThermostat()
        {            
            ThermostatDic.Clear();
            DataTable DT = MySqlHelp.GetThermostat();
            
            try
            {
                if (DT.Rows.Count == 0)  //温控器表中无数据。则进入温控器配置界面。
                {
                    FirstSetting firstSetting = new FirstSetting();
                    firstSetting.ShowDialog();
                    DT = MySqlHelp.GetThermostat();
                }
                foreach (DataRow Row in DT.Rows)
                {
                    var id = int.Parse(Row["id"].ToString());
                    var num = Row["num"].ToString();
                    var addr = byte.Parse(Row["addr"].ToString());
                    var com = Row["com"].ToString();
                    ThermostatDic.Add(id, new Thermostat(id, num, addr, com));
                }
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("forcach温控器列表失败"+ex.ToString());
            }
            
        }


        public static void NewDataEvent(int id,byte Thermostat_Addr, byte Cmd, int Data_Length, int TData)
        {          
            switch (Cmd)
            {
                case 0x03:
                    if(Data_Length == 2)
                    {
                        ThermostatDic[id].IsConnet = true;
                        ThermostatDic[id].Card.UpShowOnline(true);
                        ThermostatDic[id].Temp = TData / 10.0;                      
                        if (ThermostatDic[id].Temp > highTemp)
                        {
                            ThermostatDic[id].Card.UpShowTemp(1);
                            MySqlHelp.AddTemp(id, ThermostatDic[id].Temp,1);
                            
                        }
                        else if (ThermostatDic[id].Temp < lowTemp)
                        {
                            ThermostatDic[id].Card.UpShowTemp(2);
                            MySqlHelp.AddTemp(id, ThermostatDic[id].Temp,2);
                            
                        }
                        else
                        {
                            ThermostatDic[id].Card.UpShowTemp(0);
                            MySqlHelp.AddTemp(id, ThermostatDic[id].Temp,0);
                            
                        }                      
                    }
                    if (Data_Length == 4)
                    {
                        highTemp = (short)(TData / 65536);
                        lowTemp = (short)(TData % 65536);
                        Console.WriteLine("高温："+highTemp+"    低温："+lowTemp);
                    }
                    break;
                default:break;
            }
          
        }

        public static void FirstQuery()
        {
            try
            {
                Task t = new Task(() =>
                {
                    //MainFrom.Logger.Error("*************首次轮询开始**************");
                    //MainFrom.Logger.Error("错误：总数=" + errCount + ":" + Thermostat.revCount);
                    foreach (var Th in ThermostatDic)
                    {
                        Thread.Sleep(1000);
                        if (Th.Key <= 0) continue;
                        ThermostatSerialPort.Cmd_Operation(SP1, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                        //MainFrom.Logger.Error(Th.Key);
                        //Thread.Sleep(500);
                        if (Th.Key > 32) break;
                    }
                    //MainFrom.Logger.Error("*************首次轮询完成**************");
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("首次轮询错误！" + ex.ToString());
            }
        }

        private static void Imfor_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {                   
                    foreach (var Th in ThermostatDic)
                    {
                        if (Th.Key <= 0) continue;
                        string name = "";
                        string num = "";
                        string age = "";
                        string time = "";
                        string pic = "";
                        name = MySqlHelp.GetDefName(ThermostatDic[Th.Key].ID);
                        num = MySqlHelp.GetDefNum(ThermostatDic[Th.Key].ID);
                        age = MySqlHelp.GetDefAge(ThermostatDic[Th.Key].ID);
                        time = MySqlHelp.GetDefTime(ThermostatDic[Th.Key].ID);
                        pic = MySqlHelp.GetDefPic(ThermostatDic[Th.Key].ID);
                        ThermostatDic[Th.Key].Card.UpShowImfor(num,name,age,time,pic);

                        Thread.Sleep(800);
                        
                    }
                    //MainFrom.Logger.Error("*************刷新逝者信息成功**************");
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("刷新逝者信息成功错误！" + ex.ToString());
            }
        }

        private static void Timing1_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {               
                    //MainFrom.Logger.Error("*************第1组轮询开始**************");
                    //MainFrom.Logger.Error("错误：总数=" +errCount + ":" + Thermostat.revCount);
                    foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 0) continue;
                            ThermostatSerialPort.Cmd_Operation(SP1, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                           // MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(1500);
                            if (Th.Key > 32) break;                           
                        }
                       //MainFrom.Logger.Error("*************第1组轮询完成**************");
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第1组轮询错误！"+ex.ToString());
            }           
        }

        private static void Timing2_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
               
                        //MainFrom.Logger.Error("*************第2组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <=32) continue;
                            ThermostatSerialPort.Cmd_Operation(SP2, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 64) break;
                        }
                        //MainFrom.Logger.Error("*************第2组轮询完成**************");
                 
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第2组轮询错误！" + ex.ToString());
            }
        }
        private static void Timing3_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
                 
                        //MainFrom.Logger.Error("*************第3组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 64) continue;
                            ThermostatSerialPort.Cmd_Operation(SP3, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 96) break;
                        }
                        //MainFrom.Logger.Error("*************第3组轮询完成**************");
                   
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第3组轮询错误！" + ex.ToString());
            }
        }
        private static void Timing4_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
                  
                        //MainFrom.Logger.Error("*************第4组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 96) continue;
                            ThermostatSerialPort.Cmd_Operation(SP4, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 128) break;
                        }
                       // MainFrom.Logger.Error("*************第4组轮询完成**************");
                  
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第4组轮询错误！" + ex.ToString());
            }
        }

        private static void Timing5_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
                 
                        //MainFrom.Logger.Error("*************第5组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 128) continue;
                            ThermostatSerialPort.Cmd_Operation(SP5, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                           // MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 160) break;
                        }
                       // MainFrom.Logger.Error("*************第5组轮询完成**************");
                   
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第5组轮询错误！" + ex.ToString());
            }
        }
        private static void Timing6_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
                  
                        //MainFrom.Logger.Error("*************第6组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 160) continue;
                            ThermostatSerialPort.Cmd_Operation(SP6, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 192) break;
                        }
                        //MainFrom.Logger.Error("*************第6组轮询完成**************");
                    
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第6组轮询错误！" + ex.ToString());
            }
        }
        private static void Timing7_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
                  
                        //MainFrom.Logger.Error("*************第7组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 192) continue;
                            ThermostatSerialPort.Cmd_Operation(SP7, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 224) break;
                        }
                        //MainFrom.Logger.Error("*************第7组轮询完成**************");
                    
                });
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第7组轮询错误！" + ex.ToString());
            }
        }
        private static void Timing8_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
                   
                        //MainFrom.Logger.Error("*************第8组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 224) continue;
                            ThermostatSerialPort.Cmd_Operation(SP8, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 256) break;
                        }
                        //MainFrom.Logger.Error("*************第8组轮询完成**************");
                    }
                );
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第8组轮询错误！" + ex.ToString());
            }
        }
        private static void Timing9_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {
                   
                       // MainFrom.Logger.Error("*************第9组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 256) continue;
                            ThermostatSerialPort.Cmd_Operation(SP9, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 288) break;
                        }
                        //MainFrom.Logger.Error("*************第9组轮询完成**************");
                    }
                );
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第9组轮询错误！" + ex.ToString());
            }
        }
        private static void Timing10_Query(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Task t = new Task(() =>
                {

                        //MainFrom.Logger.Error("*************第10组轮询开始**************");
                        foreach (var Th in ThermostatDic)
                        {
                            if (Th.Key <= 288) continue;
                            ThermostatSerialPort.Cmd_Operation(SP10, Th.Key, ThermostatDic[Th.Key].Addr, 0x03, 3, 1, 2000);
                            //MainFrom.Logger.Error(Th.Key);
                            Thread.Sleep(500);
                            if (Th.Key > 320) break;
                        }
                        //MainFrom.Logger.Error("*************第10组轮询完成**************");
                    }
                );
                t.Start();
            }
            catch (Exception ex)
            {
                //MainFrom.Logger.Error("第10组轮询错误！" + ex.ToString());
            }
        }




        /// <summary>
        /// 关闭
        /// </summary>
        public static void Close()
        {
           // thermostat.DisConnect();
        }

        /// <summary>
        /// 重连
        /// </summary>
        /// <returns></returns>
        public static bool Reconnection()
        {
          //  if (IsOpen) { thermostat.DisConnect(); }
           // IsOpen = thermostat.Connect(Properties.Settings.Default.Port_COM, Properties.Settings.Default.Port_BaudRate);
            return IsOpen1;
        }

        /// <summary>
        /// 数据接收处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        public static int revCount = 0;
        public static int errCount = 0;
        private static void SP1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[32];
            int bytesToRead = SP1.BytesToRead;
            if (bytesToRead < 6)
            { 
                Thread.Sleep(50);
            }
            if (SP1.BytesToRead>=16) SP1.Read(RxBuffer, 0, bytesToRead);
            bytesToRead = SP1.BytesToRead;
            
            if (bytesToRead >= 6 && bytesToRead < 16)
            {
                SP1.Read(RxBuffer, 0, bytesToRead);
                for (int i = 0; i < bytesToRead; i++) Console.WriteLine(RxBuffer[i].ToString());

                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;
            }
        }
        /// <summary>
        /// 数据接收处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void SP2_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP2.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP2.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP3_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP3.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP3.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP4_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP4.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP4.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP5_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP5.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP5.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP6_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP6.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP6.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP7_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP7.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP7.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP8_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP8.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP8.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP9_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP9.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP9.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
        private static void SP10_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] RxBuffer = new byte[12];
            int bytesToRead = SP10.BytesToRead;
            if (bytesToRead < 6)
            {
                Thread.Sleep(50);
            }
            bytesToRead = SP10.BytesToRead;
            if (bytesToRead >= 6 && bytesToRead < 13)
            {
                revCount++;
                if (!DealData.dealData(RxBuffer)) errCount++;

            }
        }
    }
}
